"""
设备管理数据模型
对应数据库表: dev_station, dev_box, dev_rtu, dev_meter, dev_device_event, dev_device_operation_log
全部字段显式声明校验规则，统一使用 DeviceStatus 枚举，保留 ORM 兼容。
"""
from datetime import datetime
from typing import Optional

from pydantic import BaseModel, Field, ConfigDict


# ---------- 台区 ----------
class Station(BaseModel):
	"""台区数据模型"""
	id: int = Field(..., description="主键ID")
	code: str = Field(..., max_length=16, description="台区编码(T+省市区+4位流水)")
	serial_no: Optional[str] = Field(None, max_length=64, description="设备序列号")
	name: str = Field(..., max_length=64, description="台区名称")
	status: Optional[str] = Field(None, max_length=32, description="设备状态(ONLINE-在线, OFFLINE-离线, FAULT-故障)")
	capacity_kva: Optional[int] = Field(None, ge=0, description="配变容量(kVA)")
	manager_code: Optional[str] = Field(None, max_length=32, description="负责人工号")
	address_id: Optional[int] = Field(None, description="地址ID")
	created_at: datetime = Field(..., description="创建时间(UTC)")
	updated_at: datetime = Field(..., description="更新时间(UTC)")

	model_config = ConfigDict(from_attributes=True)


# ---------- 电箱 ----------
class Box(BaseModel):
	"""电箱数据模型"""
	id: int = Field(..., description="主键ID")
	station_id: int = Field(..., description="所属台区ID")
	code: str = Field(..., max_length=20, description="电箱编码(B+台区码+4位流水)")
	serial_no: Optional[str] = Field(None, max_length=64, description="设备序列号")
	station_no: Optional[str] = Field(None, max_length=64, description="台区设备序列号")
	install_date: Optional[datetime] = Field(None, description="安装日期")
	status: Optional[str] = Field(None, description="设备状态")
	manager_code: Optional[str] = Field(None, max_length=32, description="负责人工号")
	address_id: Optional[int] = Field(None, description="地址ID")
	created_at: datetime = Field(..., description="创建时间(UTC)")
	updated_at: datetime = Field(..., description="更新时间(UTC)")

	model_config = ConfigDict(from_attributes=True)


# ---------- RTU ----------
class RTU(BaseModel):
	"""RTU数据模型"""
	id: int = Field(..., description="主键ID")
	code: Optional[str] = Field(None, max_length=64, description="编号")
	serial_no: Optional[str] = Field(None, max_length=64, description="设备序列号")
	box_id: int = Field(None, description="所属电箱ID")
	box_no: Optional[str] = Field(None, max_length=64, description="电箱序列号")
	station_id: int = Field(None, description="所属台区ID")
	station_no: Optional[str] = Field(None, max_length=32, description="台区设备序列号")
	station_name: Optional[str] = Field(None, max_length=64, description="台区名称")
	model: Optional[str] = Field(None, max_length=32, description="设备型号")
	collector_type: Optional[str] = Field(None, max_length=20, description="采集器类型(Master/RTU)")
	comm_protocol: Optional[str] = Field(None, max_length=20, description="通信协议")
	uplink_type: Optional[str] = Field(None, max_length=10, description="上行类型")
	uplink_no: Optional[str] = Field(None, max_length=32, description="SIM卡号或IP地址")
	status: Optional[str] = Field(None, description="设备状态")
	manager_code: Optional[str] = Field(None, max_length=32, description="负责人工号")
	last_online: Optional[datetime] = Field(None, description="最后在线时间(UTC)")
	address_id: Optional[int] = Field(None, description="地址ID")
	created_at: datetime = Field(..., description="创建时间(UTC)")
	updated_at: datetime = Field(..., description="更新时间(UTC)")

	model_config = ConfigDict(from_attributes=True)


# ---------- 电表 ----------
class Meter(BaseModel):
	"""电表数据模型"""
	id: int = Field(..., description="主键ID")
	code: str = Field(..., max_length=24, description="电表编码(M+RTU码+相别+3位流水)")
	rtu_id: Optional[int] = Field(None, description="所属RTU ID")
	rtu_no: Optional[str] = Field(None, max_length=32, description="RTU编号")
	rtu_serial_no: Optional[str] = Field(None, max_length=64, description="电箱设备序列号")
	box_id: Optional[int] = Field(None, description="所属嗲娘 ID")
	box_no: Optional[str] = Field(None, max_length=32, description="电箱设备变化")
	box_serial_no: Optional[str] = Field(None, max_length=64, description="电箱设备序列号")
	station_id: int = Field(..., description="所属台区 ID")
	station_no: str = Field(None, max_length=32, description="台区设备序号")
	station_serial_no: Optional[str] = Field(None, max_length=64, description="提取设备序列号")
	serial_no: Optional[str] = Field(None, max_length=64, description="设备序列号")
	meter_type: Optional[str] = Field(None, max_length=20, description="电表类型")
	phase: str = Field("NONE", max_length=10, description="相别")
	ct_ratio: int = Field(1, ge=1, description="CT变比")
	pt_ratio: int = Field(1, ge=1, description="PT变比")
	init_reading: float = Field(0.0, ge=0, description="初始读数(kWh)")
	status: Optional[str] = Field(None, description="设备状态")
	commissioning_date: Optional[datetime] = Field(None, description="投运日期")
	calibration_date: Optional[datetime] = Field(None, description="最近检定日期(UTC)")
	calibration_due_date: Optional[datetime] = Field(None, description="下次检定日期(UTC)")
	address_id: Optional[int] = Field(None, description="地址ID")
	created_at: datetime = Field(..., description="创建时间(UTC)")
	updated_at: datetime = Field(..., description="更新时间(UTC)")

	model_config = ConfigDict(from_attributes=True)


# ---------- 设备事件 ----------
class DeviceEvent(BaseModel):
	"""设备事件数据模型"""
	id: int = Field(..., description="主键ID")
	target_type: str = Field(..., max_length=20, description="目标类型(STATION/BOX/RTU/METER)")
	target_id: int = Field(..., description="目标设备ID")
	target_serial_no: Optional[str] = Field(None, max_length=64, description="目标设备序列号")
	event_type: str = Field(..., max_length=20,
							description="事件类型：DeviceEventType (REPLACED/DECOMMISSIONED/FAULT/RESTORED/BOUND/UNBOUND)")
	event_time: datetime = Field(..., description="事件时间(UTC)")
	operator_id: int = Field(..., description="操作人ID")
	related_device_id: Optional[int] = Field(None, description="关联设备ID")
	old_value: Optional[str] = Field(None, max_length=32, description="旧值")
	new_value: Optional[str] = Field(None, max_length=32, description="新值")
	reason: Optional[str] = Field(None, max_length=500, description="事件原因")
	photo_files: Optional[str] = Field(None, description="现场照片ID列表(逗号分隔)")
	work_order_id: Optional[int] = Field(None, description="关联工单ID")
	details: Optional[str] = Field(None, description="其他详细信息(JSON)")
	created_at: datetime = Field(..., description="创建时间(UTC)")
	user_name: Optional[str] = Field(None, max_length=20, description="用户名")
	device_code: Optional[str] = Field(None, max_length=20, description="设备编码")

	model_config = ConfigDict(from_attributes=True)


# ---------- 设备操作日志 ----------
class DeviceOperationLog(BaseModel):
	"""设备操作日志数据模型"""
	id: int = Field(..., description="主键ID")
	target_type: str = Field(..., max_length=20, description="目标类型")
	target_id: int = Field(..., description="目标设备ID")
	op_type: str = Field(..., max_length=20, description="操作类型(CREATE/UPDATE/DELETE)")
	op_by: int = Field(..., description="操作人ID")
	op_at: datetime = Field(..., description="操作时间(UTC)")
	note: Optional[str] = Field(None, max_length=500, description="操作备注")
	old_vals: Optional[str] = Field(None, description="变更前值(JSON)")
	new_vals: Optional[str] = Field(None, description="变更后值(JSON)")

	model_config = ConfigDict(from_attributes=True)
